package com.gxnu.edu.cn.ttxxt.controller;

import com.gxnu.edu.cn.ttxxt.entity.FontSettings;
import com.gxnu.edu.cn.ttxxt.service.FontSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/font-settings")
public class FontSettingController {

    @Autowired
    private FontSettingService fontSettingService;

    @PostMapping("/")
    public ResponseEntity<?> addFontSetting(@RequestBody Map<String, Object> request) {
        Long userId = Long.parseLong(request.get("userId").toString());
        String fontFamily = request.get("fontFamily").toString();
        Integer fontSize = Integer.parseInt(request.get("fontSize").toString());
        String fontColor = request.get("fontColor").toString();
        String fontWeight = request.get("fontWeight").toString();
        String fontStyle = request.get("fontStyle").toString();
        boolean isDefault = Boolean.parseBoolean(request.get("isDefault").toString());

        FontSettings setting = fontSettingService.addFontSetting(userId, fontFamily, fontSize, fontColor,
                                                               fontWeight, fontStyle, isDefault);
        return ResponseEntity.ok(setting);
    }

    @PutMapping("/{fontSettingId}")
    public ResponseEntity<?> updateFontSetting(@PathVariable Long fontSettingId, 
                                            @RequestBody Map<String, Object> request) {
        String fontFamily = request.get("fontFamily").toString();
        Integer fontSize = Integer.parseInt(request.get("fontSize").toString());
        String fontColor = request.get("fontColor").toString();
        String fontWeight = request.get("fontWeight").toString();
        String fontStyle = request.get("fontStyle").toString();
        boolean isDefault = Boolean.parseBoolean(request.get("isDefault").toString());

        FontSettings setting = fontSettingService.updateFontSetting(fontSettingId, fontFamily, fontSize,
                                                                   fontColor, fontWeight, fontStyle, isDefault);
        return ResponseEntity.ok(setting);
    }

    @DeleteMapping("/{fontSettingId}")
    public ResponseEntity<?> deleteFontSetting(@PathVariable Long fontSettingId) {
        fontSettingService.deleteFontSetting(fontSettingId);
        return ResponseEntity.ok("字体设置已删除");
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<FontSettings> getUserFontSettings(@PathVariable Long userId) {
        FontSettings settings = fontSettingService.getUserFontSettings(userId);
        return ResponseEntity.ok(settings);
    }

    @GetMapping("/user/{userId}/default")
    public ResponseEntity<?> getDefaultFontSetting(@PathVariable Long userId) {
        FontSettings setting = fontSettingService.getDefaultFontSetting(userId);
        return ResponseEntity.ok(setting);
    }

    @PutMapping("/user/{userId}/default")
    public ResponseEntity<?> setDefaultFontSetting(@PathVariable Long userId, 
                                                 @RequestBody Map<String, Long> request) {
        Long fontSettingId = request.get("fontSettingId");
        fontSettingService.setDefaultFontSetting(userId, fontSettingId);
        return ResponseEntity.ok("默认字体设置已更新");
    }
}
    